package com.witmore.neutron.ai.mcp.server.trading.service;

import com.witmore.neutron.ai.mcp.server.trading.client.AkShareHttpClient;
import com.witmore.neutron.ai.mcp.server.trading.constants.AkShareApiConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * AKShare数据服务
 * 统一提供所有AKShare相关的数据接口
 * 
 * @author 豆子高
 * @date 2025-01-20
 */
@Slf4j
@Service
public class AkshareService {

    @Autowired
    private AkShareHttpClient akShareHttpClient;

    // ==================== 基础行情接口 ====================

    /**
     * 盘口异动接口
     */
    public String stockChangesEm(String symbol) throws IOException {
        log.info("获取盘口异动: {}", symbol);
        return akShareHttpClient.executeGetRequestRaw(AkShareApiConstants.STOCK_CHANGES_EM, symbol);
    }

    /**
     * 个股信息查询-东财
     */
    public String stockIndividualInfoEm(String symbol) throws IOException {
        log.info("获取个股信息: {}", symbol);
        return akShareHttpClient.executeGetRequestRaw(AkShareApiConstants.STOCK_INDIVIDUAL_INFO_EM, symbol);
    }

    /**
     * 实时行情数据 - 东财
     */
    public String stockRealTimeInfoEm() throws IOException {
        log.info("获取实时行情数据");
        return akShareHttpClient.executeGetRequestRaw(AkShareApiConstants.STOCK_REAL_TIME_INFO_EM);
    }



    // ==================== 财务报表相关接口 ====================

    /**
     * 获取个股研报-东方财富
     * @param symbol 股票代码
     * @return 研报数据JSON字符串
     */
    public String getStockResearchReportEm(String symbol) throws IOException {
        log.info("获取个股研报-东财: {}", symbol);
        return akShareHttpClient.executeGetRequestRaw(AkShareApiConstants.STOCK_RESEARCH_REPORT_EM, symbol);
    }

    /**
     * 获取财务报表-新浪
     * @param stock 带市场标识的股票代码 如 "sh600600"
     * @param symbol 报表类型 "资产负债表", "利润表", "现金流量表"
     * @return 财务报表数据JSON字符串
     */
    public String getStockFinancialReportSina(String stock, String symbol) throws IOException {
        log.info("获取财务报表-新浪: {} - {}", stock, symbol);
        return akShareHttpClient.executeGetRequestRaw(AkShareApiConstants.STOCK_FINANCIAL_REPORT_SINA, stock, symbol);
    }

    // ==================== 东方财富财务报表接口 ====================

    /**
     * 获取资产负债表-按报告期-东财
     * @param symbol 股票代码 如 "SH600519"
     * @return 资产负债表数据JSON字符串
     */
    public String getStockBalanceSheetByReportEm(String symbol) throws IOException {
        log.info("获取资产负债表-按报告期: {}", symbol);
        return akShareHttpClient.executeGetRequestRaw(AkShareApiConstants.STOCK_BALANCE_SHEET_BY_REPORT_EM, symbol);
    }

    /**
     * 获取资产负债表-按年度-东财
     * @param symbol 股票代码 如 "SH600519"
     * @return 资产负债表数据JSON字符串
     */
    public String getStockBalanceSheetByYearlyEm(String symbol) throws IOException {
        log.info("获取资产负债表-按年度: {}", symbol);
        return akShareHttpClient.executeGetRequestRaw(AkShareApiConstants.STOCK_BALANCE_SHEET_BY_YEARLY_EM, symbol);
    }

    /**
     * 获取利润表-按报告期-东财
     * @param symbol 股票代码 如 "SH600519"
     * @return 利润表数据JSON字符串
     */
    public String getStockProfitSheetByReportEm(String symbol) throws IOException {
        log.info("获取利润表-按报告期: {}", symbol);
        return akShareHttpClient.executeGetRequestRaw(AkShareApiConstants.STOCK_PROFIT_SHEET_BY_REPORT_EM, symbol);
    }

    /**
     * 获取利润表-按年度-东财
     * @param symbol 股票代码 如 "SH600519"
     * @return 利润表数据JSON字符串
     */
    public String getStockProfitSheetByYearlyEm(String symbol) throws IOException {
        log.info("获取利润表-按年度: {}", symbol);
        return akShareHttpClient.executeGetRequestRaw(AkShareApiConstants.STOCK_PROFIT_SHEET_BY_YEARLY_EM, symbol);
    }

    /**
     * 获取利润表-按单季度-东财
     * @param symbol 股票代码 如 "SH600519"
     * @return 利润表数据JSON字符串
     */
    public String getStockProfitSheetByQuarterlyEm(String symbol) throws IOException {
        log.info("获取利润表-按单季度: {}", symbol);
        return akShareHttpClient.executeGetRequestRaw(AkShareApiConstants.STOCK_PROFIT_SHEET_BY_QUARTERLY_EM, symbol);
    }

    /**
     * 获取现金流量表-按报告期-东财
     * @param symbol 股票代码 如 "SH600519"
     * @return 现金流量表数据JSON字符串
     */
    public String getStockCashFlowSheetByReportEm(String symbol) throws IOException {
        log.info("获取现金流量表-按报告期: {}", symbol);
        return akShareHttpClient.executeGetRequestRaw(AkShareApiConstants.STOCK_CASH_FLOW_SHEET_BY_REPORT_EM, symbol);
    }

    /**
     * 获取现金流量表-按年度-东财
     * @param symbol 股票代码 如 "SH600519"
     * @return 现金流量表数据JSON字符串
     */
    public String getStockCashFlowSheetByYearlyEm(String symbol) throws IOException {
        log.info("获取现金流量表-按年度: {}", symbol);
        return akShareHttpClient.executeGetRequestRaw(AkShareApiConstants.STOCK_CASH_FLOW_SHEET_BY_YEARLY_EM, symbol);
    }

    /**
     * 获取现金流量表-按单季度-东财
     * @param symbol 股票代码 如 "SH600519"
     * @return 现金流量表数据JSON字符串
     */
    public String getStockCashFlowSheetByQuarterlyEm(String symbol) throws IOException {
        log.info("获取现金流量表-按单季度: {}", symbol);
        return akShareHttpClient.executeGetRequestRaw(AkShareApiConstants.STOCK_CASH_FLOW_SHEET_BY_QUARTERLY_EM, symbol);
    }

    // ==================== 同花顺财务报表接口 ====================

    /**
     * 获取资产负债表-同花顺
     * @param symbol 股票代码 如 "000063"
     * @param indicator 指标类型 "按报告期", "按年度", "按单季度"
     * @return 资产负债表数据JSON字符串
     */
    public String getStockFinancialDebtThs(String symbol, String indicator) throws IOException {
        log.info("获取资产负债表-同花顺: {} - {}", symbol, indicator);
        return akShareHttpClient.executeGetRequestRaw(AkShareApiConstants.STOCK_FINANCIAL_DEBT_THS, symbol, indicator);
    }

    /**
     * 获取利润表-同花顺
     * @param symbol 股票代码 如 "000063"
     * @param indicator 指标类型 "按报告期", "按年度", "按单季度"
     * @return 利润表数据JSON字符串
     */
    public String getStockFinancialBenefitThs(String symbol, String indicator) throws IOException {
        log.info("获取利润表-同花顺: {} - {}", symbol, indicator);
        return akShareHttpClient.executeGetRequestRaw(AkShareApiConstants.STOCK_FINANCIAL_BENEFIT_THS, symbol, indicator);
    }

    /**
     * 获取现金流量表-同花顺
     * @param symbol 股票代码 如 "000063"
     * @param indicator 指标类型 "按报告期", "按年度", "按单季度"
     * @return 现金流量表数据JSON字符串
     */
    public String getStockFinancialCashThs(String symbol, String indicator) throws IOException {
        log.info("获取现金流量表-同花顺: {} - {}", symbol, indicator);
        return akShareHttpClient.executeGetRequestRaw(AkShareApiConstants.STOCK_FINANCIAL_CASH_THS, symbol, indicator);
    }

    // ==================== 公告相关接口 ====================

    /**
     * 获取沪深京A股公告
     * @param symbol 公告类型 "全部", "重大事项", "财务报告", "融资公告", "风险提示", "资产重组", "信息变更", "持股变动"
     * @param date 指定日期 格式: "20220511"
     * @return 公告数据JSON字符串
     */
    public String getStockNoticeReport(String symbol, String date) throws IOException {
        log.info("获取沪深京A股公告: {} - {}", symbol, date);
        return akShareHttpClient.executeGetRequestRaw(AkShareApiConstants.STOCK_NOTICE_REPORT, symbol, date);
    }
}
